﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Mvc;
using ClientManager.Model;
using ClientManager.Service.Clients;
using ClientManager.Service.ServiceLevels;
using ClientManager.Web.Controllers;
using ClientManager.Web.Mappings;
using ClientManager.Web.Models;
using Moq;
using NUnit.Framework;

namespace ClientManager.Web.Test
{
    [TestFixture]
    public class ClientControllerTest
    {
        private ClientController _clientController;
        private Mock<IClientService> _clientServiceMock;
        private Mock<IServiceLevelService> _serviceLevelServiceMock;
        
        [SetUp]
        public void SetUp()
        {
            _clientServiceMock = new Mock<IClientService>();
            _serviceLevelServiceMock = new Mock<IServiceLevelService>();
            _clientController = new ClientController(_clientServiceMock.Object,_serviceLevelServiceMock.Object);
            AutoMapperConfiguration.Configure();
        }

        [TearDown]
        public void TearDown()
        {
            _clientServiceMock = null;
            _serviceLevelServiceMock = null;
            _clientController = null;
        }

        [Test]
        public void Create_ModelStateHasErrors_ReturnsCreatePageWithTheErrors()
        {
            //Arrange
            _clientController.ModelState.AddModelError("error", "error");
            _serviceLevelServiceMock.Setup(x => x.GetAll()).Returns(new List<ServiceLevel>());
            //Act
            var result = _clientController.Create(new ClientViewModel()) as ViewResult;

            //Assert
            Assert.AreEqual("Create", result.ViewName);
        }

        [Test]
        public void Create_ModelStateIsValid_CreatesSuccessfullyANewClient()
        {
            //Arrange
            var clientViewModel = new ClientViewModel
            {
                ServiceLevelId = 1,
                Surname = "John",
                FirstName = "Erban",
            };
            //Act
            _clientController.Create(clientViewModel);

            //Assert
            _clientServiceMock.Verify(x=>x.Create(It.Is<Client>(c=> c.FirstName == clientViewModel.FirstName && c.Surname == clientViewModel.Surname && c.ServiceLevelId == clientViewModel.ServiceLevelId)));
        }

        [Test]
        public void Edit_ModelStateHasErrors_ReturnsEditPageWithTheErrors()
        {
            //Arrange
            _clientController.ModelState.AddModelError("error", "error");
            _serviceLevelServiceMock.Setup(x => x.GetAll()).Returns(new List<ServiceLevel>());
            //Act
            var result = _clientController.Edit(new ClientViewModel()) as ViewResult;

            //Assert
            Assert.AreEqual("Edit", result.ViewName);
        }

        [Test]
        public void Edit_ModelStateIsValid_EditsSuccessfulyANewClient()
        {
            //Arrange
            var clientViewModel = new ClientViewModel
            {
                ServiceLevelId = 1,
                Surname = "John",
                FirstName = "Erban",
            };
            //Act
            _clientController.Edit(clientViewModel);

            //Assert
            _clientServiceMock.Verify(x=>x.Update(It.Is<Client>(c=> c.FirstName == clientViewModel.FirstName && c.Surname == clientViewModel.Surname && c.ServiceLevelId == clientViewModel.ServiceLevelId)));
        }

        [Test]
        public void DeleteConfirmed_DeleteAClientWithValidId_DeletsSuccessfullyAClient()
        {
            //Arrange
            var clientId = 1;
            _clientServiceMock.Setup(x => x.GetById(1)).Returns(new Client{ClientId = 1});
            
            //Act
            _clientController.DeleteConfirmed(clientId);

            //Assert
            _clientServiceMock.Verify(x => x.Delete(It.Is<Client>(c=> c.ClientId == clientId)));
        }


    }
}
